﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FreedomX2.Core.IO;
using System.Collections;
namespace FreedomX2.Core.Transport.SocketProvider
{
    public class SocketBlocks
    {
        private SocketBlocks() { }
        public static int HeadLogoLength = 4;
        public static int HeadIDLength = 4;
        public static int FootSizeLength = 4;
        public static int FootCustomLength = 4;
        public static int ContentLength = 1024;
        public static int PackageSize
        {
            get { return ContentLength + HeadLogoLength + HeadIDLength + FootSizeLength + FootCustomLength; }
            set { ContentLength = value - HeadLogoLength - HeadIDLength - FootSizeLength - FootCustomLength; }
        }
        public byte[] bytes;
        public SocketBlocks(int bytelength)
        {
            if (bytelength < 128)
            {
                throw new Exception("Blocks Lenght must > 256 , please change that");
            }
            bytes = new byte[bytelength];
            ContentLength = bytelength - HeadLogoLength - HeadIDLength - FootSizeLength - FootCustomLength;
        }

        public static List<byte[]> CreateBlocks(string Message, out int lastSize)
        {
            List<byte[]> reList = new List<byte[]>();
            byte[] buffer = Convertor.ConvertStringToBytes(Message);
            lastSize = buffer.Length % ContentLength;
            for (int i = 0; i < buffer.Length / ContentLength + 1; i++)
            {
                int iLength = buffer.Length < lastSize ? buffer.Length : lastSize;
                byte[] bytes = new byte[ContentLength];
                Array.Copy(buffer, i * ContentLength, bytes, 0, iLength);
                reList.Add(bytes);
            }
            return reList;
        }
        public static List<byte[]> CreateBlocks(byte[] buffer, out int lastSize)
        {
            List<byte[]> reList = new List<byte[]>();
            lastSize = buffer.Length % ContentLength;
            for (int i = 0; i < buffer.Length / ContentLength + 1; i++)
            {
                int iLength = buffer.Length < lastSize ? buffer.Length : lastSize;
                byte[] bytes = new byte[ContentLength];
                Array.Copy(buffer, i * ContentLength, bytes, 0, iLength);
                reList.Add(bytes);
            }
            return reList;
        }
        public static ThreadContent GetThreadContent(SocketThread socketThread)
        {
            ThreadContent threadContent = new ThreadContent();
            threadContent.ID = socketThread.ID;
            threadContent.SerialID = socketThread.SerialID;

            ArrayList alResult = new ArrayList();
            int size = 0;
            (from v in socketThread.Blocks
             orderby v.Custom
             select v.Size).ToList().ForEach(a => { size += a; });

            var vSort = (from v in socketThread.Blocks
                         orderby v.Custom
                         select v).ToList();
            vSort.ForEach(a =>
           {
               alResult.AddRange(a.BytesList);
           });
            threadContent.Size = alResult.Count;
            byte[] bytesReault = new byte[size];
            Array.Copy(alResult.ToArray(), bytesReault, size);
            threadContent.BytesList.AddRange(bytesReault);
            return threadContent;
        }

    }
}
